<template>
  <div class="setting-view">
    <el-form :model="formData" label-width="auto" style="max-width: 400px">
      <el-form-item :label="t('setting.name')">
        <el-input
          v-model="formData.name"
          size="large"
          width="150"
          :placeholder="t('setting.name')"
          clearable
          @blur="setName"
        />
      </el-form-item>
      <el-form-item :label="t('setting.username')">
        <el-input
          v-model="formData.username"
          size="large"
          :placeholder="t('setting.username')"
          clearable
          @blur="setUsername"
        />
      </el-form-item>
      <el-form-item :label="t('setting.role')">
        {{ formData.role == 1 ? t('dashboard.admine') : t('dashboard.user') }}
      </el-form-item>
      <el-form-item :label="t('setting.phone')">
        <el-input v-model="formData.phone" size="large" :placeholder="t('setting.phone')" clearable @blur="setPhone" />
      </el-form-item>
      <el-form-item :label="t('setting.email')">
        <el-input v-model="formData.email" size="large" :placeholder="t('setting.email')" clearable @blur="setEmail" />
      </el-form-item>
      <el-form-item>
        <!-- <el-button size="large" type="primary" @click="onSubmit">确定</el-button> -->
        <el-button size="large" type="primary" @click="open = true">{{ t('setting.setPassworld') }}</el-button>
      </el-form-item>
    </el-form>
    <el-dialog v-model="open" :title="t('setting.setPassworld')" width="500" :before-close="handleClose">
      <el-input v-model="formData.password" size="large" :placeholder="t('setting.pleaseInput')" clearable />
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="open = false">{{ t('setting.cancel') }}</el-button>
          <el-button type="primary" @click="setPassword">{{ t('setting.confirm') }}</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import {
  getUsrInfo,
  changepassword,
  changeusername,
  changeemail,
  changephone,
  changename,
  changeprofile
} from '@/api/system/auth'
import { i18n } from '@/lang'
const { t } = i18n.global

const formData = ref({
  id: 0,
  role: 0,
  name: '',
  phone: '',
  email: '',
  username: '',
  password: '',
  update_at: '',
  create_at: ''
})

const open = ref(false)
const onSubmit = () => {
  console.log('submit!')
}
const handleClose = () => {}

const setName = () => {
  changename(formData.value).then((res) => {
    console.log('>>>')
  })
}
const setUsername = () => {
  changeusername(formData.value).then((res) => {
    console.log('>>>')
  })
}
const setPassword = () => {
  open.value = false
  changepassword(formData.value).then((res) => {
    console.log('>>>')
  })
}
const setPhone = () => {
  changephone(formData.value).then((res) => {
    console.log('>>>')
  })
}
const setEmail = () => {
  changeemail(formData.value).then((res) => {
    console.log('>>>')
  })
}
const setProfile = () => {
  changeprofile(formData.value).then((res) => {
    console.log('>>>')
  })
}

onMounted(() => {
  getUsrInfo().then((res) => {
    if (!res.data) return
    formData.value = res.data
  })
})
</script>

<style scoped>
.setting-view {
  padding-top: 16px;
}
</style>
